package com.github.freewu.commons.utils;


import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
* @Description: (注册倒计时) 
* @author 唐飞
* @date 2016年1月26日 上午10:52:21 *
 */
public class TimerCounUtil extends CountDownTimer {

	private TextView textView;// 按钮
	private EditText editText;
	private LinearLayout layout;

	/**
	 * 
	 * @param millisInFuture 时长
	 * @param countDownInterval 间隔
	 * @param textView 文本
	 * @param editText 手机输入框
	 */
	public TimerCounUtil(long millisInFuture, long countDownInterval, TextView textView,EditText editText,LinearLayout layout) {
		super(millisInFuture, countDownInterval);
		this.textView = textView;
		this.editText = editText;
		this.layout = layout;
	}

	@SuppressLint("NewApi")
	@Override
	public void onTick(long millisUntilFinished) {
		editText.setEnabled(false);
		layout.setEnabled(false);
		if (millisUntilFinished / 1000 > -1) {
			textView.setText("重新发送"+millisUntilFinished / 1000 + "S");// 设置倒计时时间
		}
		Spannable span = new SpannableString(textView.getText().toString());// 获取按钮的文字
		span.setSpan(new ForegroundColorSpan(0xABABAB), 0, textView.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);// 讲倒计时时间显示为红色
		textView.setText(span);
	}

	@SuppressLint("NewApi")
	@Override
	public void onFinish() {
		changState();

	}

	@SuppressLint("NewApi")
	public void changState() {
		textView.setText("重新获取验证码");
		layout.setEnabled(true);
		layout.setClickable(true);
		editText.setEnabled(true);
	}
}